﻿namespace ImageProcessing.Entities
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Image Class
    /// </summary>
    public class IMG_Image
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="IMG_Image"/> class.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        public IMG_Image(int width, int height)
        {
            this.ImagePixels = new Pixel[width * height];
            this.Width = width;
            this.Height = height;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="IMG_Image"/> class.
        /// </summary>
        /// <param name="orgImage">The org image.</param>
        public IMG_Image(Bitmap orgImage)
        {
            this.Height = orgImage.Height;
            this.Width = orgImage.Width;
            this.ImagePixels = new Pixel[this.Width * this.Height];
            Color img_Color = new Color();
            for (int i = 0; i < this.Height; i++)
            {
                for (int j = 0; j < this.Width; j++)
                {
                    int pixelLocation = (i * this.Width) + j;
                    img_Color = orgImage.GetPixel(j, i);
                    this.ImagePixels[pixelLocation] = new Pixel(img_Color.R, img_Color.G, img_Color.B);
                }
            }
        }

        /// <summary>
        /// Gets the width.
        /// </summary>
        public int Width { get; private set; }

        /// <summary>
        /// Gets the height.
        /// </summary>
        public int Height { get; private set; }

        /// <summary>
        /// Gets or sets the image pixels.
        /// </summary>
        /// <value>
        /// The image pixels -- saved column by column.
        /// </value>
        public Pixel[] ImagePixels
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the <see cref="ImageProcessing.Entities.Pixel"/> with the specified x.
        /// </summary>
        /// <returns>image pixel</returns>
        /// <param name="y">y index height</param> 
        /// <param name="x">x index width</param>
        public Pixel this[int y, int x]
        {
            get
            {
                int pixelLocation = (y * this.Width) + x;
                return this.ImagePixels[pixelLocation];
            }

            set
            {
                int pixelLocation = (y * this.Width) + x;
                this.ImagePixels[pixelLocation] = value;
            }
        }

        /// <summary>
        /// Bitmaps the of image.
        /// </summary>
        /// <returns>bitmap image</returns>
        public Bitmap BitmapOfImage()
        {
            try
            {
                Bitmap b = new Bitmap(this.Width, this.Height);
                Color tmpColor = new Color();
                Pixel tmpPixel;
                for (int i = 0; i < this.Height; i++)
                {
                    for (int j = 0; j < this.Width; j++)
                    {
                        if (this[i, j] == null)
                        {
                            tmpColor = Color.FromArgb(0, 0, 0);
                            b.SetPixel(j, i, tmpColor);
                            this[i, j] = new Pixel(0, 0, 0);
                            continue;
                        }

                        tmpPixel = this[i, j];
                        tmpColor = Color.FromArgb((int)tmpPixel.Red, (int)tmpPixel.Green, (int)tmpPixel.Blue);
                        b.SetPixel(j, i, tmpColor);
                    }
                }

                return b;
            }
            catch (Exception img_Ex)
            {
                Logger.LogException(img_Ex);
                throw;
            }
        }
    }
}
